//
//  LGPatientBreathePhotoController.m
//  HeartDisease_YHJK
//
//  Created by 1234 on 2025/10/17.
//  上传报告图片 -- 查看图片

#import "LGPatientBreathePhotoController.h"
#import "LGPhotoPickerCell.h"
#import "SDPhotoBrowser.h"
@interface LGPatientBreathePhotoController ()<UICollectionViewDelegate,UICollectionViewDataSource,SDPhotoBrowserDelegate>
/**   */
@property (nonatomic , strong)UICollectionView *collectionView;
/**   */
@property (nonatomic , strong)NSMutableArray *modelArray;
/**   */
@property (nonatomic , strong)NSMutableArray *imageArray;

@end

@implementation LGPatientBreathePhotoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"查看报告";
    [self.view addSubview:self.collectionView];
    
}

-(void)setPhotoArray:(NSArray *)photoArray{
    _photoArray = photoArray;
    self.modelArray = [NSMutableArray array];
    self.imageArray = [NSMutableArray array];
    for (NSInteger i=0; i<photoArray.count; i++) {
        HXPhotoModel *model = [[HXPhotoModel alloc]init];
        model.imageURL = [NSURL URLWithString:photoArray[i]];
        [self.modelArray addObject:model];
        __weak typeof(self) weakSelf = self;
        [[SDWebImageManager sharedManager] loadImageWithURL:model.imageURL options:SDWebImageLowPriority progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
            [weakSelf.imageArray addObject:image];
        }];
    }
    [self.collectionView reloadData];
}


- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    HXPhotoModel *model = self.modelArray[index];
    if(model.thumbPhoto){
        return model.thumbPhoto;
    }else{
        return [UIImage imageWithData:[NSData dataWithContentsOfURL:model.imageURL]];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // Set up the reuse identifier
    LGPhotoPickerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"cell" forIndexPath:indexPath];
    cell.model = self.modelArray[indexPath.item];
    cell.deleteBtn.hidden = YES;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SDPhotoBrowser *photoBrowser =[[SDPhotoBrowser alloc] init];
    photoBrowser.currentImageIndex = indexPath.item;
    photoBrowser.delegate = self;
    photoBrowser.imageCount = self.modelArray.count;
    photoBrowser.sourceImagesContainerView = self.collectionView;
    [photoBrowser show];
}


-(UICollectionView *)collectionView{
    if(!_collectionView){
        CGFloat itemWH = (Screen_W-viewPix(32))/4.0;
        UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(itemWH, itemWH);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), Screen_H-topBarHeight-viewPix(16)) collectionViewLayout:layout];
        [_collectionView registerClass:[LGPhotoPickerCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.scrollEnabled = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}

@end
